// pages/demo_searchfilter/demo_searchfilter.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 筛选条件
    table: 'e_ceshil',
    keyList: [],
    searchList: [],
    advList: [],
    
  },

  // 搜索栏
  searchVal(e) {
    console.log('搜索栏的值', e.detail);
  },
  // 快速查询
  searchQuick(e) {
    console.log('快速查询', e.detail);
  },
  // 高级查询
  searchAdv(e) {
    console.log('高级查询', e.detail);
  },
  // 混合查询
  searchComb(e) {
    console.log('搜索栏、快速、高级查询 混合查询', e.detail);
  },
  

  // 接口
  ready() {
    let { table, } = this.data,
        p0 = this.getQuickFilter(table),
        p1 = this.getAllEntityFields(table)
    // 1.获取快速查询条件，1.1获取引用筛选的主显示字段，1.2获取引用筛选的选项，2.获取所有字段得到单选多选的选项，3.获取高级查询tab
    Promise.all([p0, p1, ]).then(res => {
      let searchList = res[0],
          radioCheckList = res[1]
      
      searchList.forEach(item => {
        let { type, field_id, } = item
        if(['picklist', 'multiple', ].includes(type)) item.optionList = radioCheckList[field_id]
      })

      searchList.push({
        field_name: '单个日期',
        type: 'dateonly',
        field_id: 'aaa',
        // dfltdate: '2022-1-6'
      })

      this.setData({
        searchList,
      })
    })

    this.getAdvTab(table)
  },
  // 1.获取筛选条件项目
  getQuickFilter(tableid) {
    return new Promise((resolve, reject) => {
      wx.$post(3, 'Entity_query_model/query_entity_query_model', { table_id: tableid, }, '', true).then(res => {
        let { showFields, query_field, } = res,
            obj = {}

        this.setData({
          keyList: query_field,
        })

        showFields.forEach(item => {
          let { type, mainEntity, } = item
          if(['reference1', 'reference', ].includes(type)) {
            if(!obj[mainEntity]) obj[mainEntity] = []
            obj[mainEntity].push(item)
          }
        })
        let proArr = []
        Object.keys(obj).forEach(entity => {
          proArr.push(this.getRefOptionList(entity, obj[entity]))
        })

        Promise.all(proArr).then(res => {
          resolve(showFields)
        })
      })
    })
  },
  // 2.获取模块所有字段
  getAllEntityFields(tableid) {
    return new Promise((resolve, reject) => {
      wx.$post(4, 'QX/PRO_select_field', { table: tableid, fieldname: '', }).then(res => {
        if(res.errcode !== 0) return
        let rows = res.data.row,
            radioCheckList = {}
        rows.forEach(row => {
          let { type, field_id, } = row
          if(['picklist', 'multiple'].includes(type)) radioCheckList[field_id] = JSON.parse(row.option)
        })
        resolve(radioCheckList)
      })
    })
  },
  // 获取引用字段的选项
  getRefOptionList(tableid, arr) {
    return new Promise((resolve, reject) => {
      // 1.1获取模块的主显示字段
      let p0 = new Promise((resolve, reject) => {
        wx.$post(3, 'Entity_param/query_entity_param', { table_id: tableid, }).then(res => {
          if(res.errcode !== 0) return
          resolve(res.data.row[0].entity_key)
        })
      })
      // 1.2获取引用字段的选项
      let p1 = new Promise((resolve, reject) => {
        wx.$post(3, 'DynamicQuery/query_dynamic', { oby: `${tableid}.row_id desc`, table_id: tableid, }).then(res => {
          if(res.errcode !== 0) return
          resolve(res.data.row[0])
        })
      })

      Promise.all([p0, p1, ]).then(res => {
        let optionName = res[0],
            optionList = res[1]
        arr.forEach(item => {
          item.optionName = optionName
          item.optionList = optionList
        })
        resolve()
      })
    })
  },
  // 3.获取高级查询tab
  getAdvTab(tableid) {
    let post = {
      system_id: 800,
      page: 1,
      limit: 1000,
      oby: 'input_time',
      SearchWhere: [
        {
          CompareWith: 'table_id',
          Operator: '=',
          CompareValue: tableid,
          or: 0,
        },
      ],
    }
    wx.$post(3, 'DynamicQuery/query_personal_combination_query', post).then(res => {
      if(res.errcode !== 0) return
      this.setData({
        advList: res.data.row,
      })
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.ready()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
})